********************************************************************************
** 	TITLE:		us2020_anes_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       anes_timeseries_2020_stata_20210324                           **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2020_anes_contact_networks", replace

* Open the dataset

use "anes_timeseries_2020_stata_20210719.dta", clear


**************************
** STATE-LEVEL OUTCOMES **
**************************

* Note: 1 = Democrat, 2 = Republican

gen statewinner = .

replace statewinner = 2 if V203000 == 1
replace statewinner = 2 if V203000 == 2
replace statewinner = 1 if V203000 == 4
replace statewinner = 2 if V203000 == 5
replace statewinner = 1 if V203000 == 6
replace statewinner = 1 if V203000 == 8
replace statewinner = 1 if V203000 == 9
replace statewinner = 1 if V203000 == 10
replace statewinner = 1 if V203000 == 11
replace statewinner = 2 if V203000 == 12
replace statewinner = 1 if V203000 == 13
replace statewinner = 1 if V203000 == 15
replace statewinner = 2 if V203000 == 16
replace statewinner = 1 if V203000 == 17
replace statewinner = 2 if V203000 == 18
replace statewinner = 2 if V203000 == 19
replace statewinner = 2 if V203000 == 20
replace statewinner = 2 if V203000 == 21
replace statewinner = 2 if V203000 == 22
replace statewinner = 1 if V203000 == 23
replace statewinner = 1 if V203000 == 24
replace statewinner = 1 if V203000 == 25
replace statewinner = 1 if V203000 == 26
replace statewinner = 1 if V203000 == 27
replace statewinner = 2 if V203000 == 28
replace statewinner = 2 if V203000 == 29
replace statewinner = 2 if V203000 == 30
replace statewinner = 2 if V203000 == 31
replace statewinner = 1 if V203000 == 32
replace statewinner = 1 if V203000 == 33
replace statewinner = 1 if V203000 == 34
replace statewinner = 1 if V203000 == 35
replace statewinner = 1 if V203000 == 36
replace statewinner = 2 if V203000 == 37
replace statewinner = 2 if V203000 == 38
replace statewinner = 2 if V203000 == 39
replace statewinner = 2 if V203000 == 40
replace statewinner = 1 if V203000 == 41
replace statewinner = 1 if V203000 == 42
replace statewinner = 1 if V203000 == 44
replace statewinner = 2 if V203000 == 45
replace statewinner = 2 if V203000 == 46
replace statewinner = 2 if V203000 == 47
replace statewinner = 2 if V203000 == 48
replace statewinner = 2 if V203000 == 49
replace statewinner = 1 if V203000 == 50
replace statewinner = 1 if V203000 == 51
replace statewinner = 1 if V203000 == 53
replace statewinner = 2 if V203000 == 54
replace statewinner = 1 if V203000 == 55
replace statewinner = 2 if V203000 == 56


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by state (absolute difference between Democratic and Republican candidates)

gen margin = .

replace margin = 25.46165 if V203000 == 1
replace margin = 10.06119 if V203000 == 2
replace margin = .3087082 if V203000 == 4
replace margin = 27.62067 if V203000 == 5
replace margin = 29.16322 if V203000 == 6
replace margin = 13.40694 if V203000 == 8
replace margin = 20.07361 if V203000 == 9
replace margin = 18.96813 if V203000 == 10
replace margin = 86.75237 if V203000 == 11
replace margin = 3.358368 if V203000 == 12
replace margin = .2355843 if V203000 == 13
replace margin = 29.19466 if V203000 == 15
replace margin = 30.77116 if V203000 == 16
replace margin = 16.98819 if V203000 == 17
replace margin = 16.05948 if V203000 == 18
replace margin = 8.152966 if V203000 == 19
replace margin = 14.65296 if V203000 == 20
replace margin = 25.93505 if V203000 == 21
replace margin = 18.60943 if V203000 == 22
replace margin = 8.97435 if V203000 == 23
replace margin = 33.21037 if V203000 == 24
replace margin = 33.21483 if V203000 == 25
replace margin = 2.783527 if V203000 == 26
replace margin = 7.110157 if V203000 == 27
replace margin = 16.54535 if V203000 == 28
replace margin = 15.39087 if V203000 == 29
replace margin = 16.3691 if V203000 == 30
replace margin = 19.05753 if V203000 == 31
replace margin = 2.390533 if V203000 == 32
replace margin = 7.351566 if V203000 == 33
replace margin = 15.93768 if V203000 == 34
replace margin = 10.79262 if V203000 == 35
replace margin = 22.93059 if V203000 == 36
replace margin = 1.348122 if V203000 == 37
replace margin = 33.35729 if V203000 == 38
replace margin = 8.031963 if V203000 == 39
replace margin = 33.08709 if V203000 == 40
replace margin = 16.08607 if V203000 == 41
replace margin = 1.164883 if V203000 == 42
replace margin = 20.80582 if V203000 == 44
replace margin = 11.67664 if V203000 == 45
replace margin = 26.16414 if V203000 == 46
replace margin = 23.20886 if V203000 == 47
replace margin = 5.57859 if V203000 == 48
replace margin = 20.48379 if V203000 == 49
replace margin = 35.07472 if V203000 == 50
replace margin = 10.11401 if V203000 == 51
replace margin = 19.20332 if V203000 == 53
replace margin = 38.93503 if V203000 == 54
replace margin = .6270981 if V203000 == 55
replace margin = 43.11192 if V203000 == 56


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen surveydate = date(V203055,"YMD")

format %tdMon_DD,_CCYY surveydate

gen edate = 20201103

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = electiondate - surveydate

* Gender

gen male = V201600

recode male (-9=.)
recode male (2=0)

* Age

gen age = V201507x

recode age (-9=.)

* Education

gen education = V201511x

recode education (-9=.)
recode education (-8=.)
recode education (-2=.)

* Interest for election

gen interest = V201006

recode interest (-9=.)

replace interest = 4 - interest

* Caring about outcome (national)

gen care_whole = V201216

recode care_whole (-9=.)
recode care_whole (-8=.)

replace care_whole = 6 - care_whole

* Closeness of election (national)

gen closeness_whole = V201218

recode closeness_whole (-9=.)
recode closeness_whole (-8=.)
recode closeness_whole (-1=.)

recode closeness_whole (1=0)
recode closeness_whole (2=1)

* Closeness of election (state)

gen closeness_state = V201220

recode closeness_state (-9=.)
recode closeness_state (-8=.)
recode closeness_state (-1=.)

recode closeness_state (1=0)
recode closeness_state (2=1)

* Party identification scale (national)

gen pidscale = V201231x

recode pidscale (-9=.)
recode pidscale (-8=.)

gen pidscale_whole = 8 - pidscale

label define pidscale 1 "Strong loser PID" 2 "Weak loser PID" 3 "Leaner loser PID" 4 "No PID" 5 "Leaner winner PID" 6 "Weak winner PID" 7 "Strong winner PID"
label values pidscale_whole pidscale

* Party identification scale (state)

gen pidscale_state = pidscale if statewinner == 2
replace pidscale_state = 8 - pidscale if statewinner == 1

label values pidscale_state pidscale

* Political news attentiveness

gen news = V202407

recode news (-9=.)
recode news (-8=.)
recode news (-7=.)
recode news (-6=.)
recode news (-5=.)

replace news = 5 - news

* Senate term length - Correct answer (6 years) = 1, Incorrect = 0

gen length = V201644

recode length (-9=.)
recode length (-5=.)

replace length = 0 if length!=6 & length!=.
replace length = 1 if length == 6

* Program on which federal government spend the least

gen least = V201645

recode least (-9=.)
recode least (-5=.)

replace least = 0 if least!=1 & least!=.
replace least = 1 if least == 1

* Party with the most members in the U.S. House before election - Correct answer (Democrat) = 1, Incorrect answer = 0

gen house = V201646

recode house (-9=.)
recode house (-5=.)

recode house (2=0)

* Party with the most members in the U.S. Senate before election - Correct answer (Republican) = 1, Incorrect answer = 0

gen senate = V201647

recode senate (-9=.)
recode senate (-5=.)

recode senate (1=0)
recode senate (2=1)

* Mike Pence - Correct answer (U.S. vice-president) = 1, Incorrect answer = 0

gen pence = V202138y

recode pence (-7=.)
recode pence (-6=.)
recode pence (-5=.)

* Nancy Pelosi - Correct answer (Speaker of the House of Representatives) = 1, Incorrect answer = 0

gen pelosi = V202139y1

recode pelosi (-7=.)
recode pelosi (-6=.)
recode pelosi (-5=.)

* Angela Merkel - Correct answer (German Chancellor) = 1, Incorrect answer = 0

gen merkel = V202140y1

recode merkel (-7=.)
recode merkel (-6=.)
recode merkel (-5=.)

* Vladimir Putin - Correct answer (Russian President) = 1, Incorrect answer = 0

gen putin = V202141y1

recode putin (-7=.)
recode putin (-6=.)
recode putin (-5=.)

* John Roberts - Correct answer (Supreme Court Chief Justice) = 1, Incorrect answer = 0

gen roberts = V202142y2

recode roberts (-7=.)
recode roberts (-6=.)
recode roberts (-5=.)

* Political knowledge index

gen knowledge = length + least + house + senate + pence + pelosi + merkel + putin + roberts

* Political discussion

gen discussion = V202023

recode discussion (-9=.)
recode discussion (-8=.)
recode discussion (-7=.)
recode discussion (-6=.)
recode discussion (-1=.)

replace discussion = 0 if V202022 == 2

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts (1 = Democrat, 2 = Republican, 5 = other)

gen forecast_whole = V201217

recode forecast_whole (-9=.)

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=1 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 1


******************************
** STATE-LEVEL EXPECTATIONS **
******************************

* Forecasts (1 = Democrat, 2 = Republican, 3 = other)

gen forecast_state = V201219

recode forecast_state (-9=.)
recode forecast_state (-8=.)

replace forecast_state = 3 if forecast_state > 2

* Identify correct forecasts

gen correct_state_d = 0 if forecast_state!=statewinner & forecast_state!=.
replace correct_state_d = 1 if forecast_state == statewinner & forecast_state!=.


**********
** SAVE **
**********

save "us2020_anes.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion pidscale_whole interest news knowledge care_whole i.closeness_whole age i.male education time
estimates table, star(.05 .01 .001)

margins, at(pidscale_whole=1) atmeans predict(pr) // Average marginal effects
margins, at(pidscale_whole=7) atmeans predict(pr) // Average marginal effects

tab correct_whole_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

quietly logistic correct_whole_d discussion pidscale_whole interest news knowledge care_whole i.closeness_whole age i.male education time

estpost tabstat correct_whole_d discussion pidscale_whole interest news knowledge care_whole closeness_whole age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2020_anes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2020 American presidential election (ANES), national level") replace

eststo clear


***************************
** MAIN ANALYSES (STATE) **
***************************

* Regression analysis and percentage of correct and incorrect forecasts (state)

melogit correct_state_d discussion pidscale_state interest news knowledge i.closeness_state age i.male education time margin || V203000: , or
estimates table, star(.05 .01 .001)

margins, at(pidscale_state=1) atmeans predict(pr) // Average marginal effects
margins, at(pidscale_state=7) atmeans predict(pr) // Average marginal effects

tab correct_state_d if e(sample) == 1


********************************
** SUMMARY STATISTICS (STATE) **
********************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_state "State PID"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable care_whole "Caring about outcome"
label variable closeness_whole "Perceived closeness"
label variable closeness_state "Perceived closeness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

estpost tabstat correct_state_d discussion pidscale_state interest news knowledge closeness_state age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2020_anes_summary_state.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2020 American presidential election (ANES), state level") replace

eststo clear

log close